#include "CommsHandlerAll.h"

extern "C" {
#include <tct.h>
#include <trt.h>
#include <pct.h>
#include <pform.h>
#include <msg.h>
#include <print.h>
#include <fields.h>
#include <comm.h>
#include <ui.h>
#include <stdio.h>
}



RetryXTimesCommsHandler::RetryXTimesCommsHandler()
    : SoftPayBaseCommsHandler()
{
    reconnectCountConstant = 3;
    changeFromPrimaryToSecondaryAt = 1;
}

void RetryXTimesCommsHandler::setRetryValues(int reconnectCountConstant,int changeFromPrimaryToSecondaryAt) {
    this->reconnectCountConstant = reconnectCountConstant;
    this->changeFromPrimaryToSecondaryAt = changeFromPrimaryToSecondaryAt;
}

void RetryXTimesCommsHandler::initConnectSequence() {
    reconnectCount = reconnectCountConstant;     // should get from table but for the moment this will do
    firstDial = 1;
    SoftPayBaseCommsHandler::initConnectSequence();
}

int RetryXTimesCommsHandler::isRetryConnect() {
    if (clearSeen)
        return FALSE;
    return (reconnectCount--) > 1;
}

int RetryXTimesCommsHandler::attemptConnect() {
    int retVal = TRUE;

    if (!firstDial)   // not first time
    {
        SVC_WAIT(3000);
        obCommunication.inFlush();
        pobTran->fPredialed = 0;
    } else
        firstDial = 0;
    if (obCommunication.inBegin && !pobTran->fPredialed) {
        VS_BOOL prim = VS_TRUE;

        if (reconnectCount <= changeFromPrimaryToSecondaryAt)
            prim = VS_FALSE;
        if ((connectStatus = obCommunication.inBegin(&obCommunication, inGetHostHandle(), prim)) < VS_SUCCESS)
            retVal = ERROR_OCCURRED;
    }
    pobTran->fPredialed = VS_FALSE;
    return retVal;
}



